<template>
  <div>
    <el-container>
      <el-header>
        <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect">
          <el-menu-item>
            <router-link to="/index">商品管理</router-link>
          </el-menu-item>
          <el-menu-item>
            <router-link to="/users">用户管理</router-link>
          </el-menu-item>
          <el-menu-item>
            <router-link to="/shop">结算中心</router-link>
          </el-menu-item>
          <el-menu-item>
            <router-link to="/transaction">订单管理</router-link>
          </el-menu-item>
        </el-menu>
      </el-header>

      <el-main>
        <router-view/>
      </el-main>

    </el-container>
    <!--  选择购买商品-->
    <el-table :height="400"
              :data="tableData.filter(data => !search || data.g_name.toLowerCase().includes(search.toLowerCase()))"
              max-height="500"
              style="width: 100%">
      <el-table-column
        label="ID"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.g_id }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="商品名称"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.g_name }}</span>
        </template>
      </el-table-column>
      <!--      prop 绑定一列中要展示的值,这个值是后台传来json数据的值-->
      <el-table-column label="商品价格" prop="g_price">
      </el-table-column>
      <!--    <el-table-column label="商品数量" prop="g_count"></el-table-column>-->
      <!--    <el-table-column label="商品生产日期" prop="g_prodate"></el-table-column>-->
      <!--    <el-table-column label="商品过期日期" prop="g_expdate"></el-table-column>-->
      <el-table-column label="商品描述" prop="g_describe"></el-table-column>
      <el-table-column label="商品类型" prop="g_type"></el-table-column>
      <el-table-column label="" prop="">
        <el-popover
          slot-scope="scope"
          placement="right"
          width="400"
          trigger="click">请输入数量:
          <el-input-number v-model="num" @change="handleChange" :min="1" :max=scope.row.g_count
                           label="请输入数量:"></el-input-number>
          <el-button slot="reference">购买</el-button>
          <el-button :plain="true" type="primary" @click="buyGoods(num,scope.row.g_id,scope.row.g_price );open()">确定
          </el-button>
        </el-popover>
      </el-table-column>
      <el-table-column label="" align="right">
        <!--        放入搜索框-->
        <template slot="header" slot-scope="scope">
          <el-input
            v-model="search"
            size="mini"
            placeholder="请输入商品名称关键字搜索"/>
        </template>
      </el-table-column>
    </el-table>
    <span style="margin-right:5%;">
    <h3 style="width: 20%">总金额：{{ money }}</h3>
    <el-button type="success" @click="rest">结算</el-button>
    </span>
  </div>
</template>

<script>
export default {
  name: 'Shop',
  data () {
    return {
      money: 0,
      num: 1,
      search: '',
      tableData: [],
      show: true,
      form: {
        g_id: '',
        g_name: '',
        g_price: '',
        g_count: '0',
        g_prodate: '',
        g_expdate: '',
        g_describe: '',
        g_type: ''
      },
      msg: '',
      status: Boolean
    }
  },
  methods: {
    rest () {
      this.money = 0
      alert('结算完成')
    },
    open () {
      this.$message(this.msg)
    },
    handleChange (value) {
      console.log(value)
    },
    buyGoods (num, gid, price) {
      this.axios.get('http://localhost:8989/goods/buyGoods?gid=' + gid + '&num=' + num).then(res => {
        this.result = res.data
        this.msg = this.result.msg
      })
      this.money += (price * num)
    },
    findAllGoods () {
      this.axios.get('http://localhost:8989/goods/findAll').then(res => {
        console.log(res)
        this.tableData = res.data
      })
    }
  },
  created () {
    this.findAllGoods()
  }
}
</script>

<style scoped>

</style>
